class Solution:
    def lengthOfLIS(self, nums: list) -> int:
        length, res, j = len(nums), 0, 0
        if length <= 1:
            return length
        while j < length - 1 - res:
            mid_num = nums[j:]
            mid_num[0] = [1, mid_num[0], 0]
            for i in range(1, len(mid_num)):
                if mid_num[i] > mid_num[i - 1][1]:
                    mid_num[i] = [mid_num[i - 1][0] + 1, mid_num[i], mid_num[i - 1][1]]
                elif mid_num[i - 1][2] < mid_num[i] < mid_num[i - 1][1]:
                    mid_num[i] = [mid_num[i - 1][0], mid_num[i], mid_num[i - 1][2]]
                else:
                    mid_num[i] = mid_num[i - 1]
            res = max(mid_num[-1][0], res)
            if res <= 2:
                break
            mid = nums[j]
            try:
                while nums[j] >= mid:
                    j += 1
            except:
                break
        return res


a = Solution()
print(a.lengthOfLIS([10, 9, 2, 5, 3, 7, 101, 18]))
print(a.lengthOfLIS([3,5,6,2,5,4,19,5,6,7,12]))
